unit RttiUtils;

interface

uses Rtti, TypInfo;

function FieldTypeKind(AData: TObject; AFieldName: string): TTypeKind;
function ObjectInstance(AQualifiedName: string): TObject;

implementation

function FieldTypeKind(AData: TObject; AFieldName: string): TTypeKind;
var
  RttiConext: TRttiContext;
  RttiType: TRttiType;
  RttiField: TRttiField;
begin
  if not Assigned(AData) then
    Exit(tkUnknown);

  RttiType := RttiConext.GetType(AData.ClassType);
  RttiField := RttiType.GetField(AFieldName);
  if not Assigned(RttiField) then
    Exit(tkUnknown);

  Result := RttiField.FieldType.TypeKind;
end;

function ObjectInstance(AQualifiedName: string): TObject;
var
  RttiContext: TRttiContext;
  RttiType: TRttiType;
  RttiMethod: TRttiMethod;
  MetaClass: TClass;
begin
  Result := nil;

  RttiType := RttiContext.FindType(AQualifiedName);
  for RttiMethod in RttiType.GetMethods do
  begin
    if RttiMethod.HasExtendedInfo and RttiMethod.IsConstructor then
    begin
      if Length(RttiMethod.GetParameters) = 0 then
      begin
        MetaClass := RttiType.AsInstance.MetaclassType;
        Result := RttiMethod.Invoke(MetaClass, []).AsObject;
        Break;
      end;
    end;
  end;
end;

end.
